﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PROJECT_BOMBER.BLL
{
    public class Camera2d
    {

        #region Constructeur

        /// <summary>
        /// Initializes a new instance of the <see cref="Camera2d"/> class.
        /// </summary>
        /// <param name="graphics">The graphics device manager.</param>
        /// <param name="zoom">The zoom.</param>
        public Camera2d(GraphicsDeviceManager graphics, float zoom)
        {
            _zoom = zoom;
            _rotation = 0.0f;
            _pos = new Vector2((float)graphics.PreferredBackBufferWidth / (2 * Zoom), 50);
        }

        #endregion

        #region Zoom

        protected float _zoom;
        /// <summary>
        /// Gets or sets the zoom.
        /// </summary>
        /// <value>
        /// The zoom.
        /// </value>
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
        }

        #endregion

        #region Rotation

        protected float _rotation;

        /// <summary>
        /// Gets or sets the rotation.
        /// </summary>
        /// <value>
        /// The rotation.
        /// </value>
        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        #endregion

        #region Position

        public Vector2 _pos;
        /// <summary>
        /// Gets or sets the camera position
        /// </summary>
        /// <value>
        /// The camera position
        /// </value>
        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }

        #endregion

        #region Matrice

        public Matrix _transform;

        /// <summary>
        /// Create the matrix with the position, the rotation and the zoom
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <returns></returns>
        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
                _transform =
                  Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
                                             Matrix.CreateRotationZ(Rotation) *
                                             Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                             Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f, graphicsDevice.Viewport.Height * 0.5f, 0));
                return _transform;
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the camera position with the character's translation
        /// </summary>
        /// <param name="graphics">The graphics.</param>
        /// <param name="character">The character.</param>
        public void Update(GraphicsDeviceManager graphics, Character character)
        {
            if (character.Position.X > character.MinX + graphics.PreferredBackBufferWidth / (2 * Zoom) - character.Width / 2 && character.Position.X < character.MaxX - graphics.PreferredBackBufferWidth / (2 * Zoom) + character.Width / 2)
            {
                Pos = new Vector2(character.Position.X + character.Width / 2, Pos.Y);
            }
            else if (character.Position.X - character.MinX < graphics.PreferredBackBufferWidth / (2 * Zoom))
            {
                Pos = new Vector2(character.MinX + graphics.PreferredBackBufferWidth / (2 * Zoom), Pos.Y);
            }
            else if (character.MaxX - character.Position.X < graphics.PreferredBackBufferWidth / (2 * Zoom))
            {
                Pos = new Vector2(character.MaxX - graphics.PreferredBackBufferWidth / (2 * Zoom) + character.Width, Pos.Y);
            }



            if (character.Position.Y < character.MinY - graphics.PreferredBackBufferHeight / (2 * Zoom) - character.Height / 2 && character.Position.Y > -character.MaxY + graphics.PreferredBackBufferHeight / (2 * Zoom) - character.Height / 2)
            {
                Pos = new Vector2(Pos.X, character.Position.Y + character.Height / 2);
            }
            else if (character.MinY - character.Position.Y <= graphics.PreferredBackBufferHeight / (2 * Zoom))
            {
                Pos = new Vector2(Pos.X, character.MinY - graphics.PreferredBackBufferHeight / (2 * Zoom));
            }
            else if (character.Position.Y + character.MaxY < graphics.PreferredBackBufferHeight / (2 * Zoom))
            {
                Pos = new Vector2(Pos.X, -character.MaxY + graphics.PreferredBackBufferHeight / (2 * Zoom));
            }
        }

        #endregion

    }
}
